package com.rdc.qingkou.view.fragment;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.rdc.qingkou.R;
import com.rdc.qingkou.ui.SubscribeActivity;

public abstract class SubscribeFragmentBase extends Fragment {

    private RecyclerView mSubscribeRecyclerView;
    private SubscribeActivity mActivity;
    protected SwipeRefreshLayout swipeRefresh;
    private View mView;
    public TextView subscribe_total_textView;

    @Override
    public void onAttach(Context context){
        super.onAttach(context);
        mActivity = (SubscribeActivity) getActivity();

//        mPresenter= getInstance();
//        mPresenter.attachView(this);
        Log.d("BaseActivity",""+this);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        mView=inflater.inflate(R.layout.fragment_subscribe, container, false);
        initViews(mView);
        return mView;
    }

    //刷新数据
    protected abstract void refreshSubscribes();

    private void initViews(View view) {

        subscribe_total_textView=(TextView) view.findViewById(R.id.subscribe_total_textView);
        swipeRefresh=(SwipeRefreshLayout) view.findViewById(R.id.subscribe_swipeRefreshLayout);
        swipeRefresh.setColorSchemeResources(R.color.colorPrimary);
        swipeRefresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                refreshSubscribes();
            }
        });

        mSubscribeRecyclerView = (RecyclerView) view.findViewById(R.id.subscribe_recyclerView);
        LinearLayoutManager layoutManager=new LinearLayoutManager(mActivity, LinearLayoutManager.VERTICAL, false);
        mSubscribeRecyclerView.setLayoutManager(layoutManager);

        mSubscribeRecyclerView.setOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                subscribe_total_textView.setVisibility(View.INVISIBLE);
                LinearLayoutManager manager = (LinearLayoutManager) recyclerView.getLayoutManager();
                // 当不滚动时
                if (newState == RecyclerView.SCROLL_STATE_IDLE) {
                    //获取最后一个完全显示的ItemPosition
                    int lastVisibleItem = manager.findLastCompletelyVisibleItemPosition();
                    int totalItemCount = manager.getItemCount();

                    // 判断是否滚动到底部
                    if (lastVisibleItem == (totalItemCount - 1) ) {
                        subscribe_total_textView.setVisibility(View.VISIBLE);
                    }
                }
            }

            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
            }
        });

        setSubscribeRecyclerViewAdapter();
    }


    //为SubscribeRecyclerView设置Adapter
    //SubscribeRecyclerView由父类的getSubscribeRecyclerView()提供。
    protected abstract void setSubscribeRecyclerViewAdapter();

    protected final RecyclerView getSubscribeRecyclerView() {
        return mSubscribeRecyclerView;
    }
}
